<template>
  <div class="login">
    <img class="login-img" src="@/assets/loginBg.jpg" alt="">
    <el-card>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
        <el-form-item>
          <div class="head-title">彩票管理系统</div>
        </el-form-item>
        <el-form-item prop="name">
          <el-input auto-complete="off" clearable placeholder="请输入用户名" v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input auto-complete="off" clearable type="password" placeholder="请输入密码" v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="submit" type="primary" @click="submitForm">登 录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import { mapActions } from 'vuex'
export default {
  name: 'login',
  data () {
    return {
      ruleForm: {
        name: '',
        password: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { pattern: /admin/, message: '用户名不正确', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /111111/, message: '密码不正确', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    ...mapActions({
      login: 'login/getLogin'
    }),
    submitForm () {
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          const { name, password } = this.ruleForm
          const params = {
            username: name,
            password: password
          }
          this.login(params).then(res => {
            localStorage.setItem('user', JSON.stringify(res))
            this.$message.success('登录成功')
            this.$router.push('/')
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style lang="less">
  .login {
    width: 100%;
    height: 100%;
    background-color: #000000;
    position: relative;
    .login-img {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
    }
    .submit {
      border-color: #ffffff;
      background: none;
    }
    .el-card {
      width: 300px;
      z-index: 2;
      background: #2780c9;
      opacity: 0.7;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      border: none;
    }
    .head-title {
      color: #ffffff;
      font-size: 18px;
      text-align: center;
    }
    .el-checkbox {
      color: #ffffff;
    }
    .el-form-item__content {
      text-align: left;
      .el-button {
        width: 260px;
      }
    }
  }
</style>
